<?php

namespace app\admin\model\user;

use app\common\model\TimeModel;
use app\common\model\Invite;
/**
 * @property int $id ID 
 * @property string $user_account 账号 
 * @property string $user_nickname 昵称 
 * @property string $password 密码 
 * @property string $pay_password 支付密码 
 * @property int $user_pid 用户上级 
 * @property string $groups 多级分销 
 * @property int $level 邀请层级 
 * @property string $user_group 用户类型 
 * @property string $mobile 手机号 
 * @property string $invite_code 邀请码 
 * @property string $area_code 地区代码 
 * @property string $avatar 头像 
 * @property int $gender 性别 0:未知,1:男,2:女
 * @property string $email 邮箱 
 * @property string $birthday 生日 
 * @property string $user_level 用户等级 
 * @property int $last_login_time 最后登录时间 
 * @property int $last_login_ip 最后登录ip 
 * @property int $create_time 创建时间 
 * @property int $status 状态 0:禁用,1:启用
 * @property string $client_id app的client_id 
 * @property int $user_auth 所属角色权限组 
 * @property int $is_del 是否删除 0:否,1:是
 * @property string $source 用户注册平台 iOS:苹果APP,Android:安卓APP,Web:H5终端,Wechat:微信公众平台,Alipay:支付宝平台,Baidu:百度平台,ByteBounce:字节跳动平台,QQ:QQ平台,FastApp:快应用,360:360平台,other:其他平台
 */
class User extends TimeModel
{

    protected $name = "user";

    protected $deleteTime = false;

    
    public const SELECT_LIST_GENDER = ['1'=>'男','2'=>'女',];

    public const SELECT_LIST_STATUS = ['0'=>'忙碌','1'=>'空闲',];

    public const SELECT_LIST_IS_DEL = ['0'=>'否','1'=>'是',];

    public const SELECT_LIST_SOURCE = ['iOS'=>'苹果APP','Android'=>'安卓APP','Web'=>'H5终端','Wechat'=>'微信公众平台','Alipay'=>'支付宝平台','Baidu'=>'百度平台','ByteBounce'=>'字节跳动平台','QQ'=>'QQ平台','FastApp'=>'快应用','360'=>'360平台','other'=>'其他平台',];

    /** 
     * @Author: Bryant  
     * @Date: 2023-06-12 15:30:13 
     * @method post 
     * @title 保存用户注册信息 
     */
    public function get_reg_data($data)
    {
        $user_data['user_pid'] = 0;
        $user_data['groups'] = '';
        if (isset($data['invite_code'])) {
            //获取上级id，上级parent
            $parent = self::where('invite_code', $data['invite_code'])->find();
            if ($parent) {
                $user_data['user_pid'] = $parent['id'];
                $user_data['parent_pid'] = $parent['user_pid'];
                $groups = $parent['groups'];
                if ($groups) {
                    $user_data['groups'] = $groups  . ',' . $parent['id'];
                    $user_data['level'] = $parent['level'] + 1;
                }
            }
        }
        $user_data['password']      = password($data['password']);
        $user_data['mobile']        = $data['mobile'];
        $user_data['email']        = isset($data['email']) ? $data['email'] : '';
        $user_data['avatar']        = isset($data['avatar']) ? $data['avatar'] : '';
        $user_data['user_nickname'] = $data['user_nickname'] ? $data['user_nickname'] : $this->get_name();
        $user_data['create_time']   = time();
        $user_data['status']        = isset($data['status']) ? $data['status'] : 1;
        $user_data['sex']           = $data['sex'] ?? 0;
        $user_data['birthday'] = isset($data['birthday']) ? strtotime($data['birthday']) : '';

        $user_data['start_price']           = $data['start_price'] ?? 0;
        $user_data['user_type']           = $data['user_type'] ?? 1;
        $user_data['images']           = $data['images'] ?? 0;
        $user_data['image']           = $data['image'] ?? 0;
        $user_data['talent']           = $data['talent'] ?? 0;
        $user_data['weight']           = $data['weight'] ?? 0;
        $user_data['height']           = $data['height'] ?? 0;
        $user_data['drving_age']           = $data['drving_age'] ?? 0;
        $user_data['online_status']           = $data['online_status'] ?? 0;
        $user_data['is_certify']           = $data['is_certify'] ?? 0;
        $user_data['lat']           = $data['lat'] ?? 0;
        $user_data['lng']           = $data['lng'] ?? 0;
        $user_data['lng']           = $data['lng'] ?? 0;
        $user_data['refresh_num'] = sysconfig('user','refresh_num');
        //注册账号
        $result  = self::create($user_data);
        $user_id = $result->id;
        if (!$user_id) {
            exception('注册会员失败');
        }
        // 更新用户邀请码
        $userinfo = self::update(['invite_code' => Invite::getUserId($user_id)], ['id' => $user_id]);
        if(isset($data['openid_type']) && isset($data['openid'])){
            //添加用户三方信息
            $params = [
                'user_id' => $user_id,
                'openid' => $data['openid'],
                'type' => $data['openid_type'],
                'create_time' => time()
            ];
            \think\facade\Db::name('user_third')->insert($params);
        }
        //添加用户资产表
        \think\facade\Db::name('user_assets')->insert(['user_id' => $user_id]);
        if (!$userinfo) {
            exception('注册附加信息失败');
        }
        // 提交事务
        return $user_id;
    }
    public function get_name()
    {
        $length = 6;
        $data = "abcefghijklmnpqrstuvwxyz123456789";
        return substr(str_shuffle($data),0,$length);
    }
    

}